﻿using System.Xml.Linq;
using Engine;

namespace Game
{
    public class TradingWidget : CanvasWidget
    {
        public ComponentMoney m_componentInventory;

        public ComponentHealth m_componentHealth;

        public ComponentPlayer m_componentPlayer;

        public InventorySlotWidget m_craftingRemainsSlot;

        public GridPanelWidget m_inventoryGrid;

        public GridPanelWidget m_chestGrid;

        public ButtonWidget m_itemsButton;

        public ButtonWidget m_shopButton;

        public ButtonWidget m_moneyButton;

        public BlockIconWidget m_itemvalueicon;

        public ButtonWidget m_prevRecipeButton;

        public ButtonWidget m_nextRecipeButton;

        public SliderWidget m_countSlider;

        public Money m_money;

        public int itemvalue = 0;

        public int itemcount = 0;

        public TradingWidget(IInventory inventory, ComponentMoney componentInventory, ComponentPlayer componentPlayer, ComponentHealth componentHealth)
        {
            m_componentInventory = componentInventory;
            m_componentHealth = componentHealth;
            m_componentPlayer = componentPlayer;
            XElement node = ContentManager.Get<XElement>("Widgets/TradingWidget");
            LoadContents(this, node);
            m_inventoryGrid = Children.Find<GridPanelWidget>("InventoryGrid");
            m_chestGrid = Children.Find<GridPanelWidget>("ChestGrid");
            m_craftingRemainsSlot = Children.Find<InventorySlotWidget>("CraftingRemainsSlot");
            m_itemsButton = Children.Find<ButtonWidget>("ItemsButton");
            m_shopButton = Children.Find<ButtonWidget>("ShopButton");
            m_moneyButton = Children.Find<ButtonWidget>("MoneyButton");
            m_itemvalueicon = Children.Find<BlockIconWidget>("Icon");
            m_countSlider = Children.Find<SliderWidget>("CountSlider");
            m_prevRecipeButton = Children.Find<ButtonWidget>("PreviousRecipe");
            m_nextRecipeButton = Children.Find<ButtonWidget>("NextRecipe");
            m_countSlider.MinValue = 0f;
            m_countSlider.MaxValue = 40f;
            m_countSlider.Granularity = 1f;
            m_countSlider.Value = itemcount;
            int num = 0;
            for (int i = 0; i < m_chestGrid.RowsCount; i++)
            {
                for (int j = 0; j < m_chestGrid.ColumnsCount; j++)
                {
                    InventorySlotWidget inventorySlotWidget = new InventorySlotWidget();
                    inventorySlotWidget.AssignInventorySlot(componentInventory, num++);
                    m_chestGrid.Children.Add(inventorySlotWidget);
                    m_chestGrid.SetWidgetCell(inventorySlotWidget, new Point2(j, i));
                }
            }

            num = 10;
            for (int k = 0; k < m_inventoryGrid.RowsCount; k++)
            {
                for (int l = 0; l < m_inventoryGrid.ColumnsCount; l++)
                {
                    InventorySlotWidget inventorySlotWidget2 = new InventorySlotWidget();
                    inventorySlotWidget2.AssignInventorySlot(inventory, num++);
                    m_inventoryGrid.Children.Add(inventorySlotWidget2);
                    m_inventoryGrid.SetWidgetCell(inventorySlotWidget2, new Point2(l, k));
                }
            }

            m_craftingRemainsSlot.AssignInventorySlot(componentInventory, componentInventory.RemainsSlotIndex);
        }

        public override void Update()
        {
            int num = MathUtils.Clamp((int)m_countSlider.Value, 0, 40);
            if (m_componentHealth.Health <= 0f)
            {
                base.ParentWidget.Children.Remove(this);
            }

            m_countSlider.Text = "数量:" + num;
            if (!m_componentInventory.IsAddedToProject)
            {
                base.ParentWidget.Children.Remove(this);
            }

            if (m_itemsButton.IsClicked)
            {
                DialogsManager.ShowDialog(null, new MoneyEditDialog(delegate (int value)
                {
                    itemvalue = value;
                }));
            }

            m_itemvalueicon.Value = itemvalue;
            int value2 = m_componentInventory.m_slots[0].Value;
            int count = m_componentInventory.m_slots[0].Count;
            int value3 = m_componentInventory.m_slots[m_componentInventory.RemainsSlotIndex].Value;
            int count2 = m_componentInventory.m_slots[m_componentInventory.RemainsSlotIndex].Count;
            if (count2 <= 0 && value3 != 0)
            {
                m_componentInventory.m_slots[m_componentInventory.RemainsSlotIndex].Value = 0;
            }

            if (m_shopButton.IsClicked)
            {
                foreach (Money recipe in MoneyManager.m_recipes)
                {
                    if (itemvalue != recipe.ResultValue)
                    {
                        continue;
                    }

                    if (value2 == 987)
                    {
                        if (value3 == 0 || value3 == recipe.ResultValue)
                        {
                            if (count >= num * recipe.itemMoney)
                            {
                                m_componentInventory.m_slots[0].Count -= recipe.itemMoney * num;
                                m_componentInventory.m_slots[m_componentInventory.RemainsSlotIndex].Value = recipe.ResultValue;
                                m_componentInventory.m_slots[m_componentInventory.RemainsSlotIndex].Count += num;
                            }
                            else
                            {
                                m_componentPlayer.ComponentGui.DisplaySmallMessage("金额不足", Color.White, blinking: true, playNotificationSound: true);
                            }
                        }
                        else
                        {
                            m_componentPlayer.ComponentGui.DisplaySmallMessage("输出的糟出现了问题", Color.White, blinking: true, playNotificationSound: true);
                        }
                    }
                    else
                    {
                        m_componentPlayer.ComponentGui.DisplaySmallMessage("货币无效", Color.White, blinking: true, playNotificationSound: true);
                    }
                }
            }

            m_prevRecipeButton.IsEnabled = m_countSlider.Value > 0f;
            m_nextRecipeButton.IsEnabled = m_countSlider.Value < 40f;
            if (m_prevRecipeButton.IsClicked)
            {
                m_countSlider.Value--;
            }

            if (m_nextRecipeButton.IsClicked)
            {
                m_countSlider.Value++;
            }

            if (!m_moneyButton.IsClicked)
            {
                return;
            }

            foreach (Money recipe2 in MoneyManager.m_recipes)
            {
                if (value2 == recipe2.ResultValue)
                {
                    if (value3 == 0 || value3 == 987)
                    {
                        m_componentInventory.m_slots[0].Count = 0;
                        m_componentInventory.m_slots[m_componentInventory.RemainsSlotIndex].Value = 987;
                        m_componentInventory.m_slots[m_componentInventory.RemainsSlotIndex].Count += count * recipe2.returnMoney;
                    }
                    else
                    {
                        m_componentPlayer.ComponentGui.DisplaySmallMessage("输出的糟出现了问题", Color.White, blinking: true, playNotificationSound: true);
                    }
                }
            }
        }
    }
}